package com.wjm.blog.core.swagger;

import com.wjm.blog.common.base.SysUserDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Swagger配置
 *
 * @author jiyuan
 * @version 1.0
 * @date 2020/4/20 11:44
 */
@ConditionalOnExpression("${platform.xian.swagger.enable:true}")
@Configuration
@EnableSwagger2
@Slf4j
public class SwaggerConfig implements ApplicationRunner {

    @Value("${platform.xian.swagger.base-package:com}")
    private String basePackage = "";

    @Value("${server.port:80}")
    private int port;
    @Value("${server.servlet.context-path:/service/101}")
    private String contextPath;

    @Autowired
    private SwaggerProperties swaggerProperties;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
//                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()
                //忽略swagger请求不需要参数显示
                .ignoredParameterTypes(SysUserDetailVo.class, HttpSession.class, HttpServletRequest.class, HttpServletResponse.class);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .contact(swaggerProperties.getContact())
                .version(swaggerProperties.getVersion())
                .build();
    }

    private List<Parameter> getGlobalOperationParameters() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder p = new ParameterBuilder();
        p.name("Authorization").description("token").defaultValue("")
                .modelRef(new ModelRef("string")).parameterType("header").required(false);
        pars.add(p.build());
        p.name("USER_AUTH").description("auth").defaultValue("")
                .modelRef(new ModelRef("string")).parameterType("header").required(false);
        pars.add(p.build());
        return pars;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String swaggerUrl = String.format("http://localhost:%s%sdoc.html", port, contextPath);
        log.info("swagger url => {}", swaggerUrl);
    }

}
